package com.ureport.test.utils;

import com.bstek.ureport.export.ExportConfigureImpl;
import com.bstek.ureport.export.ExportManager;
import com.sun.deploy.net.URLEncoder;

import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Map;
import java.util.Objects;

public class ExportUtils {
    public static void exportPdf(ExportManager exportManager,String sourcePath, Map<String, Object> param, String fileName, HttpServletResponse response) throws Exception {
        try {
            // 设置响应头信息
            response.setContentType("application/pdf");
            response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));

            // 获取响应的输出流
            OutputStream outputStream = response.getOutputStream();

            try {
                ExportConfigureImpl exportConfigure = new ExportConfigureImpl(sourcePath, param, outputStream);
                exportManager.exportPdf(exportConfigure); // 使用exportPdf方法导出PDF
            } catch (Exception e) {
                throw new Exception("导出pdf错误", e);
            } finally {
                if (outputStream != null) {
                    try {
                        outputStream.close();
                    } catch (Exception e) {
                        throw new Exception("导出pdf错误", e);
                    }
                }
            }
        } catch (Exception e) {
            throw new Exception("导出pdf错误", e);
        }
    }

    public static void exportExcel(ExportManager exportManager, String sourcePath, Map<String, Object> param,String fileName,HttpServletResponse response) throws Exception {
        try {
            // 设置响应头信息
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));

            // 获取响应的输出流
            OutputStream outputStream = response.getOutputStream();

            try {
                String ext = fileName.substring(fileName.lastIndexOf(".") + 1);
                ExportConfigureImpl exportConfigure = new ExportConfigureImpl(sourcePath, param, outputStream);
                if (Objects.equals(ext, "xls")) {
                    exportManager.exportExcel97(exportConfigure);
                } else {
                    if (!Objects.equals(ext, "xlsx")) {
                        throw new Exception("文件名不符合!");
                    }
                    exportManager.exportExcel(exportConfigure);
                }
            } catch (Exception e) {
                throw new Exception("导出excel错误", e);
            } finally {
                if (outputStream != null) {
                    try {
                        outputStream.close();
                    } catch (Exception e) {
                        throw new Exception("导出excel错误", e);
                    }
                }
            }
        } catch (Exception e) {
            throw new Exception("导出excel错误", e);
        }
    }

}